#! /usr/bin/python
# coding=utf-8

from tkinter import *
import tkinter.messagebox as messagebox


class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack(padx=80, ipady=35)
        self.createWidgets()

    def createWidgets(self):

        locksLabel = Label(self, text='lock', width=10, height=2)
        locksLabel.grid(row=0, column=1, padx=10, pady=10)
        stocksLabel = Label(self, text='stock')
        stocksLabel.grid(row=0, column=3, padx=10, pady=10)
        barrelsLabel = Label(self, text='barrel')
        barrelsLabel.grid(row=0, column=5, padx=10, pady=10)

        self.locksInput = Entry(self)
        self.locksInput.grid(row=1, column=1, padx=10, ipady=5)
        self.stocksInput = Entry(self, bg='white')
        self.stocksInput.grid(row=1, column=3, padx=10, ipady=5)
        self.barrelsInput = Entry(self, bg='white')
        self.barrelsInput.grid(row=1, column=5, padx=10, ipady=5)
        labal = Label(self, text="")
        labal.grid(row=2, column=3, padx=10, pady=10)
        alrtButton = Button(self, text='发送电报', command=self.submit)
        alrtButton.grid(row=3, column=3, ipadx=8, ipady=5)
        button1 = Button(self, text="退出", command=self.quit)
        button1.grid(row=3, column=5, ipadx=4, ipady=1)

    def submit(self):
        global suml, sums, sumb
        if self.locksInput.get() == '':
            locks = 0
        else:
            locks = self.locksInput.get()
        if self.stocksInput.get() == '':
            stocks = 0
        else:
            stocks = self.stocksInput.get()
        if self.barrelsInput.get() == '':
            barrels = 0
        else:
            barrels = self.barrelsInput.get()
        try:
            locks = int(locks)
            stocks = int(stocks)
            barrels = int(barrels)
            if locks < -1:
                messagebox.showinfo(
                    'Error', '您此次发送的lock的销售数据有误，请核对后重新输入。若您想重新输入本月已经输入过的数据，请点击“退出”按钮，并重新启动本软件进行输入。')
            elif stocks < 0:
                messagebox.showinfo(
                    'Error', '您此次发送的stock的销售数据有误，请核对后重新输入。若您想重新输入本月已经输入过的数据，请点击“退出”按钮，并重新启动本软件进行输入。')
            elif barrels < 0:
                messagebox.showinfo(
                    'Error', '您此次发送的barrel的销售数据有误，请核对后重新输入。若您想重新输入本月已经输入过的数据，请点击“退出”按钮，并重新启动本软件进行输入。')
            elif (suml + locks) > 70:
                messagebox.showinfo(
                    'Error', '加上此次的销售数据，您本月的lock的总销售数量已经超过单月最大允许量，请核对后重新输入。若您想重新输入本月已经输入过的数据，请点击“退出”按钮，并重新启动本软件进行输入。')
            elif (sums + stocks) > 80:
                messagebox.showinfo(
                    'Error', '加上此次的销售数据，您本月的stock的总销售数量已经超过单月最大允许量，请核对后重新输入。若您想重新输入本月已经输入过的数据，请点击“退出”按钮，并重新启动本软件进行输入。')
            elif (sumb + barrels) > 90:
                messagebox.showinfo(
                    'Error', '加上此次的销售数据，您本月的barrel的总销售数量已经超过单月最大允许量，请核对后重新输入。若您想重新输入本月已经输入过的数据，请点击“退出”按钮，并重新启动本软件进行输入。')
            elif locks == -1:
                button2 = Button(self, text="获取报告", command=self.report)
                button2.grid(row=3, column=5, ipadx=15, ipady=8)
                return suml, sums, sumb
            else:
                suml = suml + locks
                sums = sums + stocks
                sumb = sumb + barrels
                self.locksInput.delete(0, END)
                self.stocksInput.delete(0, END)
                self.barrelsInput.delete(0, END)
        except:
            messagebox.showinfo(
                'Error', '您此次发送的销售数据有误，请核对后重新输入。若您想重新输入本月已经输入过的数据，请点击“退出”按钮，并重新启动本软件进行输入。')

    def report(self):
        sum1, sum2, sum3 = self.submit()
        sale = sum1 * 45 + sum2 * 30 + sum3 * 25
        if (sale > 1800):
            commission = (sale - 1800) * 0.2 + 800 * 0.15 + 1000 * 0.1
        elif(sale > 1000 and sale <= 1800):
            commission = (sale - 1000) * 0.15 + 1000 * 0.1
        else:
            commission = sale * 0.1

        popup = Toplevel(self)
        popup.wm_attributes("-topmost", 1)
        popup.title("Sales report")
        popup.geometry('800x400')
        la1 = Label(popup, text="Lock", height=2)
        la1.grid(row=0, column=1, ipadx=65, ipady=10)
        la2 = Label(popup, text="Stock")
        la2.grid(row=0, column=2, ipadx=60, ipady=10)
        la3 = Label(popup, text="Barrel")
        la3.grid(row=0, column=3, ipadx=60, ipady=10)
        la4 = Label(popup, text="The sum")
        la4.grid(row=1, column=0, ipadx=40, ipady=20)
        la5 = Label(popup, text="%d" % sum1)
        la5.grid(row=1, column=1, ipadx=65, ipady=20)
        la6 = Label(popup, text="%d" % sum2)
        la6.grid(row=1, column=2, ipadx=60, ipady=20)
        la7 = Label(popup, text="%d" % sum3)
        la7.grid(row=1, column=3, ipadx=60, ipady=20)
        la8 = Label(popup, text="Total amount")
        la8.grid(row=3, column=0, ipadx=40, ipady=15)
        la9 = Label(popup, text="", bg="white")
        la9.grid(row=3, column=1, ipadx=85, ipady=15)
        la10 = Label(popup, text="%d $" % sale, bg="white")
        la10.grid(row=3, column=2, ipadx=85, ipady=15)
        la11 = Label(popup, text="", bg="white")
        la11.grid(row=3, column=3, ipadx=85, ipady=15)
        la12 = Label(popup, text="Commission")
        la12.grid(row=4, column=0, ipadx=40, ipady=20)

        if sum1 == 0 or sum2 == 0 or sum3 == 0:
            la13 = Label(popup, text="No commission")
            la13.grid(row=4, column=2, ipadx=60, ipady=20)
        else:
            la13 = Label(popup, text="%d $" % commission)
            la13.grid(row=4, column=2, ipadx=60, ipady=20)
        la14 = Label(popup, text="").grid(row=6, column=2, ipadx=40, ipady=20)
        button3 = Button(popup, text="退出", command=popup.quit)
        button3.grid(row=7, column=2, ipadx=40, ipady=10)


app = Application()
# 设置窗口标题:
app.master.title('The Commission Program')
suml = sums = sumb = 0
# 主消息循环:
app.mainloop()
